//
//  ViewController2.m
//  20101
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()<UITableViewDataSource,UITabBarDelegate>
@property (weak, nonatomic) IBOutlet UITextField *stuId;
@property (weak, nonatomic) IBOutlet UITextField *stuName;
@property (strong, nonatomic) IBOutlet UITextField *stuAge;
@property(nonatomic,strong)NSMutableArray *stulist;
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@end

@implementation ViewController2
-(NSMutableArray *)stulist
{
    if(_stulist==nil)
    {
        NSFileManager *fm=[NSFileManager defaultManager];
        if([fm fileExistsAtPath:[self plistpath]])
        {
            _stulist=[NSMutableArray arrayWithContentsOfFile:[self plistpath]];
            
        }
        else
        {
            _stulist=[NSMutableArray array];
        }
    }
    return _stulist;
}
-(NSString *)plistpath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filepath = [docPath stringByAppendingPathComponent:@"students.plist"];
    return filepath;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)saveplist
{
    [self.stulist writeToFile:[self plistpath] atomically:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)add:(id)sender
{
    NSMutableDictionary *dict = [@{@"stuId":self.stuId.text,
                                   @"stuName":self.stuName.text,
                                   @"stuAge":self.stuAge.text} mutableCopy];
    [self.stulist addObject:dict];
    [self saveplist];
    [self reload];
}
- (IBAction)remove:(id)sender
{
    for(NSMutableDictionary *dict in self.stulist)
    {
        if([dict[@"stuId"] isEqualToString:self.stuId.text])
        {
            [self.stulist removeObject:dict];
            break;
        }
    }
    [self saveplist];
    [self reload];

}
- (IBAction)search:(id)sender
{
    for(NSMutableDictionary *dict in self.stulist)
    {
        if([dict[@"stuId"] isEqualToString:self.stuId.text])
        {
            dict[@"stuName"] = self.stuName.text;
            dict[@"stuAge"] = self.stuAge.text;
            break;
        }
    }
    [self saveplist];
}
- (IBAction)change:(id)sender
{
    for(NSMutableDictionary *dict in self.stulist)
    {
        if([dict[@"stuId"] isEqualToString:self.stuId.text])
        {
            self.stuName.text = dict[@"stuName"];
            self.stuAge.text = dict[@"stuAge"];
            break;
        }
    }
    [self reload];
}
- (IBAction)all:(id)sender
{
    for(NSMutableDictionary *dict in self.stulist)
    {
        NSLog(@"%@ %@ %@",dict[@"stuId"],dict[@"stuName"],dict[@"stuAge"]);
    }
}
-(void)reload
{
    [self.tableview reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.stulist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSDictionary *d=self.stulist[indexPath.row];
    cell.textLabel.text=[NSString stringWithFormat:@"ID:%@,姓名:%@,年龄:%@",d[@"stuId"],d[@"stuName"],d[@"stuAge"]];
    
    return cell;
}


@end
